﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HKEA.CMS.Core.IDAL.Stg;
using HKEA.CMS.Core.Models;
using HKEA.CMS.Core.Utility;
using HKEA.CMS.Core.DALFactory;
using System.Linq.Dynamic;
namespace HKEA.CMS.Core.DAL.Stg
{
    public class stg_tblCust_MarketDAL:Istg_tblCust_Market
    {
        HKEA_CMSEntities model;
        stg_tblCust_Market source;

        public stg_tblCust_MarketDAL()
        {
            model = DataAccess.GetModelInstance();
        }

        public static stg_tblCust_MarketDAL GetInstance()
        {
            return new stg_tblCust_MarketDAL();
        }

        public int InsertENT(stg_tblCust_Market ent)
        {
            ent.CreationDate = DateTime.Now;
            ent.LastModifyDate = DateTime.Now;
            model.stg_tblCust_Market.AddObject(ent);
            return model.SaveChanges();
        }

        public List<stg_tblCust_Market> GetListByENT(stg_tblCust_Market ent, PageParam param, string strPropertyName,string strOrderBy)
        {
            var result = model.stg_tblCust_Market.Where(a => a.CustMarketUID > 0);
            if (ent.CustMarketUID > 0)
                result = result.Where(a => a.CustMarketUID == ent.CustMarketUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            param.Count = result.Count();
            result = result.OrderBy(strPropertyName + " " + strOrderBy).Skip((param.CurrentPage - 1) * param.PageSize).Take(param.PageSize);
            return result.ToList();
        }
        public List<stg_tblCust_Market> GetListByENT(stg_tblCust_Market ent)
        {
            var result = model.stg_tblCust_Market.Where(a => a.CustMarketUID > 0);
            if (ent.CustMarketUID > 0)
                result = result.Where(a => a.CustMarketUID == ent.CustMarketUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.ToList();
        }
        public int GetCountByENT(stg_tblCust_Market ent)
        {
            var result = model.stg_tblCust_Market.Where(a => a.CustMarketUID > 0);
            if (ent.CustMarketUID > 0)
                result = result.Where(a => a.CustMarketUID == ent.CustMarketUID);
            if (!string.IsNullOrEmpty(ent.LastModifyBy))
                result = result.Where(a => a.LastModifyBy == ent.LastModifyBy);
            return result.Count();
        }

        public int UpdateENT(stg_tblCust_Market ent)
        {
            source = model.stg_tblCust_Market.Where(a=>a.CustMarketUID==ent.CustMarketUID).FirstOrDefault();
            if (source != null)
            {
                source.Deleted = ent.Deleted;
                source.LastModifyBy = ent.LastModifyBy;
                source.LastModifyDate = DateTime.Now;
                source.CountryUID = ent.CountryUID;
                source.CustomerUID = ent.CustomerUID;
                source.Remarks = ent.Remarks;
            }
            return model.SaveChanges();
        }

        public int DeleteENT(stg_tblCust_Market ent)
        {
            var temp = model.stg_tblCust_Market.Where(a => a.CustMarketUID > 0);
            if (ent.CustMarketUID > 0)
                temp = temp.Where(a=>a.CustMarketUID==ent.CustMarketUID);
            var list = temp.ToList();
            for (int i = 0; i < list.Count(); i++)
            {
                model.stg_tblCust_Market.DeleteObject(list[i]);
            }
            return model.SaveChanges();
        }
    }
}
